﻿using System;
using System.Collections.Generic;
using SmartContentModule.CssModel;
using SmartContentModule.Helpers;

namespace SmartContentModule.VersionManagement
{
    internal class VersionManager   
    {
        /// <summary>
        /// Get content file from url. If not found, generate a new cssfileinfo
        /// </summary>
        /// <param name="url">url</param>
        /// <returns>Returns temp file path</returns>
        public static string GetContentFile(string url)
        {
            //check if exists
            string tempPath = CssFileVersion.GetTempFileForFileInfo(url);
            if (!String.IsNullOrEmpty(tempPath))
            {
                tempPath = GenerateFileInfo(url).TempPath;
            }

            return tempPath;
        }

        /// <summary>
        /// Gets temp path for version
        /// </summary>
        /// <param name="version">version to find in table</param>
        /// <returns>Returns temp file path</returns>
        public static string GetContentFilePathForVersion(string version)
        {
            CssFileInfo info = CssFileVersion.GetFileInfoForVersion(version);
            return info == null ? String.Empty : info.TempPath;
        }

        /// <summary>
        /// Gets handler url for css file
        /// </summary>
        /// <param name="cssUrl"></param>
        /// <returns></returns>
        public static string GetHandlerUrlForCss(string cssUrl)
        {
            string format = "<link type=\"text/css\" rel=\"Stylesheet\" href=\"{0}.axd?Target={1}\" />";
            CssFileInfo info = CssFileVersion.GetFileInfoForUrl(cssUrl);
            if (info == null)
            {
                info = GenerateFileInfo(cssUrl);
            }

            //update version if changed (before worker thread does)
            string version = info.GetVersion();
            List<string> versions = CssFileVersion.GetVersionsUsingFileInfoUrl(cssUrl);
            if (!versions.Contains(version))
            {
                info.Update();
                CssFileVersion.AddVersion(version, info.ContentUrl);
            }
            return String.Format(format, cssUrl, version);
        }

        /// <summary>
        /// Generates new CssFileInfo object. Add this object to table.
        /// </summary>
        /// <param name="url">Url to generate CssFileInfo. This url is also used as key in versions</param>
        /// <returns>Returns generated object.</returns>
        private static CssFileInfo GenerateFileInfo(string url)
        {
            //Create
            string directory = IOHelper.GetDirectoryForUrl(url);
            CssFileInfo info = new CssFileInfo(url, directory);

            //Add to table
            CssFileVersion.AddNewCssFileUrl(url, info);

            //Add to versions
            CssFileVersion.AddVersion(info.GetVersion(), url);

            return info;
        }
    }
}
